define(['jquery', 'domReady!', 'appLocalStorage', 'appConst', 'model/global', 'business/analysis/report/historyBiz', 'business/analysis/report/yieldBiz', 'business/searchbar/searchbarBiz','model/testData', 'stringUtils', 'dateUtils'], function ($, domReady, appLocalStorage, appConst, global, history, yield, searchbar, testData, stringUtils, dateUtils) {
    var that;
    var flag = {
        yield: true,
        history: false
    };
    var data = {
        searchFields:{}
    };

    var components = {

    };

    return {
        init: function () {
            that = this;
            components = {
                "tab": $('#myTab2'),
                "toggleTab": $('a[data-toggle="tab"]')
            };
            that.initTab();
            history.init();
            yield.init();
        },
        searchEventListener: function (searchFields) {
            data.searchFields=searchFields;
            flag.history = true;
            flag.yield = true;
            if (stringUtils.isBlank(data.searchFields.search_projectId)) {
                return;
            }

            if (components.tab.find(".active").find("a").attr("href") == "#s1") {
                if (stringUtils.isBlank(data.searchFields.search_startTime) && stringUtils.isBlank(data.searchFields.search_stopTime)) {
                    var obj = {
                        "startTime": dateUtils.getYesterdayDateTime,
                        "endTime": dateUtils.getCurrentDateTime
                    };
                    searchbar.setFieldsValue(obj);
                    data.searchFields.search_startTime = obj.startTime;
                    data.searchFields.search_stopTime = obj.endTime;
                }
                yield.loadYieldData(data.searchFields);
                flag.yield = false;
            } else if (components.tab.find(".active").find("a").attr("href") == "#s2") {
                history.loadSNData(data.searchFields);
                flag.history = false;
            }

        },
        initTab: function () {
            components.toggleTab.on('shown.bs.tab', function (e) {
                if (($(this).attr("href") == "#s1") && (flag.yield)) {
                    yield.loadYieldData(data.searchFields);
                    flag.yield = false;
                } else if (($(this).attr("href") == "#s2") && (flag.history)) {
                    history.loadSNData(data.searchFields);
                    flag.history = false;
                }
            });
        }
    }
});